package com.bright.cwduijie.duijie.service.impl;

import com.bright.cwduijie.duijie.enums.DataApi;
import com.bright.cwduijie.duijie.enums.OpType;
import com.bright.cwduijie.duijie.enums.SJJHBZ;
import com.bright.cwduijie.duijie.pojo.bo.ApiDataCollection;
import com.bright.cwduijie.duijie.pojo.dto.AssistCodeTypeDto;
import com.bright.cwduijie.duijie.pojo.po.AssistCode;
import com.bright.cwduijie.duijie.pojo.po.AssistCodeType;
import com.bright.cwduijie.duijie.pojo.query.DuijieQuery;
import com.bright.cwduijie.duijie.repository.AssistCodeRepository;
import com.bright.cwduijie.duijie.repository.AssistCodeTypeRepository;
import com.bright.cwduijie.duijie.service.AbstractPerThousandApiService;
import com.bright.cwduijie.duijie.util.ListUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.function.Function;

/**
 * @Author hxj
 * @Date 2024/7/29 16:25
 * @Description
 */

@Component
@RequiredArgsConstructor
@Slf4j
public class ApiAssistCodeTypeServiceImpl extends AbstractPerThousandApiService<AssistCodeType> {

    private final AssistCodeTypeRepository repo;

    @Override
    public JpaSpecificationExecutor getJpaSpecificationExecutor() {
        return repo;
    }

    @Override
    public DataApi getDataApi() {
        return DataApi.ASSIST_CODE_TYPE;
    }

    @Override
    public Function<AssistCodeType, Integer> getIdFunc() {
        return AssistCodeType::getID;
    }

    @Override
    public Function<AssistCodeType, String> getDataKeyFunc() {
        return AssistCodeType::getDATA_KEY;
    }

    @Override
    public Function<ApiDataCollection, List<AssistCodeType>> getDataFunc() {
        return ApiDataCollection::getAssistCodeTypes;
    }

    @Override
    protected DataApi getDeleteDataApi() {
        return DataApi.DELETE_ASSIST_CODE_TYPE;
    }


    @Override
    protected void getData(ApiDataCollection apiDataCollection, DuijieQuery duijieQuery) {
//        List<AssistCodeType> assets = repo.findAll(getDataQuerySpecification(OpType.ADD_OR_EDIT.getValue(), duijieQuery.getCreditCode()));
//        List<AssistCodeType> types = repo.getByZtDataKeyAndOpTypeAndSjjhbz(duijieQuery.getZtDataKey(), OpType.ADD_OR_EDIT.getValue(),
//                Arrays.asList(SJJHBZ.WAITING.getValue(), SJJHBZ.ERROR.getValue()));
        apiDataCollection.setAssistCodeTypes(getNeedDuijieData(duijieQuery));
    }

    @Override
    public void sendData(ApiDataCollection apiDataCollection, DuijieQuery duijieQuery, String batchId) throws JsonProcessingException {
        // 由于建表的时候主表子表建成了一张表 所以发送数据的时候要另外打包
        List<AssistCodeType> types = getDataFunc().apply(apiDataCollection);

        List<AssistCodeTypeDto> dataList = new ArrayList<>();

        Map<String, List<Map<String, Object>>> assistCodeTypeMap = new HashMap<>(); // key=辅助核算项目类型名  value=自定义字段集合
        for (AssistCodeType type : types) {
            if (!assistCodeTypeMap.containsKey(type.getFZHS_LX_NAME())) {
                List<Map<String, Object>> assistFieldsList = new ArrayList<>();
                assistCodeTypeMap.put(type.getFZHS_LX_NAME(), assistFieldsList);
                AssistCodeTypeDto assistCodeTypeDto = new AssistCodeTypeDto();
                assistCodeTypeDto.setDATA_KEY(type.getDATA_KEY());
                assistCodeTypeDto.setZT_DATA_KEY(type.getZT_DATA_KEY());
                assistCodeTypeDto.setTYSHXYDM(type.getTYSHXYDM());
                assistCodeTypeDto.setFZHS_LX_NAME(type.getFZHS_LX_NAME());
                assistCodeTypeDto.setFZHS_FIELD_MAP(assistFieldsList);
                dataList.add(assistCodeTypeDto);
            }
            Map<String, Object> keyValueMap = new HashMap<>();
            keyValueMap.put("FIELD_KEY", type.getFIELD_KEY());
            keyValueMap.put("FIELD_NAME", type.getFIELD_NAME());
            keyValueMap.put("FIELD_SEQ", type.getFIELD_SEQ());
            assistCodeTypeMap.get(type.getFZHS_LX_NAME()).add(keyValueMap);
        }

        int count = 0;
        if (dataList.size() == 0) {
            // 没数据的情况
            sendListData(types, Collections.emptyList(), batchId, duijieQuery.getAccSet());
        }
        while (count < dataList.size()) {
            List<AssistCodeTypeDto> subList = ListUtil.getSubList(dataList, count, count+1000);
            sendListData(subList, ListUtil.toSingleList(types, AssistCodeType::getID), batchId, duijieQuery.getAccSet());

            count += 1000;
        }

        saveSendDataKeys(dataList, AssistCodeTypeDto::getDATA_KEY);
    }

//    @Override
//    protected List<AssistCodeType> getNeedDeleteData(DuijieQuery duijieQuery) {
//        return repo.findAll(getDataQuerySpecification(OpType.DEL.getValue(), duijieQuery.getCreditCode()));
//    }

    @Override
    protected String getCheckDuplicateGroupByFields() {
        return "DATA_KEY, FIELD_KEY";// 一个主表唯一 一个子表唯一
    }
}
